#pragma once

#include "event_manager.h"
#include "../event/key_event.h"
#include "../../container/bit_set.h"

OPEN_JLIB_NS


struct KeyManager : EventManager {

  using EventManager::EventManager;

  void dispatchDown (KeyEvent::Key key, u16 modifiers);
  void dispatchRepeat (KeyEvent::Key key, u16 modifiers);
  void dispatchUp (KeyEvent::Key key, u16 modifiers);

private:
  inline void dispatch (const KeyEvent &event) {
    for (auto iter = _queue.next(); iter != &_queue; ++iter) {
      auto entry = iter->cast(&Entry::joint);
      auto node = entry->node.ptr();
      if (node->isKeyEnabled() && node->onKey(event))
        break;
    }
  }

private:
  BitSet<KeyEvent::KeyCount> _status;
};


CLOSE_JLIB_NS
